<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" @click="dialogVisible = true" size="mini">添加分类</el-button>
      </div>
      <el-table style="width: 100%;" :data="cateList" border stripe>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template #default='{row}'>
          <el-button type="primary" size="mini"  @click="getDataId(row.id)"> 修改</el-button>
          <el-button type="danger" size="mini" @click="delDataId(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  <!--   添加分类弹层 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogVisible"
      @close="resetFields"
      width="35%">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="70px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="ruleForm.cate_name" minlength="1" maxlength="10"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="ruleForm.cate_alias" minlength="1" maxlength="15"></el-input>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" >取 消</el-button>
        <el-button type="primary" @click="addCate" >添加</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹层 -->
    <el-dialog title="修改文章分类" 
      :visible.sync="editVisible" width="35%">
     <!-- 修改的表单 -->
          <el-form :model="editForm" :rules="rules" ref="editFormRef" label-width="100px">
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="editForm.cate_name"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input v-model="editForm.cate_alias"></el-input>
            </el-form-item>
          </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"  @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data(){
    return{
      editVisible : false,
      dialogVisible: false,
      cateList:[],
      ruleForm:{
        cate_name:'',
        cate_alias:''
      },
  // 存储修改文章分数据对象
      editForm:{
        cate_name: "",
        cate_alias: "",  
      },
      rules:{
        cate_name:[
        { required: true, message: '请输入分类名称', trigger: 'blur' },
        { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias:[
        { required: true, message: '请输入分类别名', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
    }
  },
  created() {
  this.ArtList()
  },
  methods:{
    //删除分类
      delDataId(id){
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const { data: res } = await this.$axios.delete("/my/cate/del", { params: { id }});
          if (res.code !== 0) return this.$message.error("删除分类失败！");
          this.$message.success("删除分类成功！");
          this.ArtList();
        }).catch((err) => err);
    },
    //修改表单
    editCate(){
        this.$refs.editFormRef.validate(async (valid)=>{
        if (!valid) return
        const {data:res} = await this.$axios.put('/my/cate/info',this.editForm)
        if(res.code!=0)return this.$message.error(res.message)
        this.$message.success(res.message)
        this.ArtList()
        this.editVisible = false
      })
    },
    //获取列表id 渲染弹窗表格
  async  getDataId(id){
      const {data:res} = await this.$axios.get('/my/cate/info',{params:{id}})
      if(res.code===0){
        this.editForm=res.data
        this.editVisible=true
      }
    },
    //重置添加分类弹窗表单
    resetFields(){
      this.$refs.ruleForm.resetFields()
    },
    //添加文章分类
    addCate(){
      this.$refs.ruleForm.validate(async (valid)=>{
        if (!valid) return
        const {data:res} = await this.$axios.post('/my/cate/add',this.ruleForm)
        if(res.code!=0)return this.$message.error(res.message)
        this.$message.success(res.message)
        this.ArtList()
        this.dialogVisible = false
      })
    },
    //获取分类信息
    async ArtList() {
    const { data: res } = await this.$axios.get('/my/cate/list')
        // console.log(res);
      if (res.code === 0) {
      this.cateList = res.data
    }
  }
  }
}

</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>